// var BASE_API_URL = "http://192.168.40.22:9081/api";
var BASE_API_URL = "http://api.fm.umakr.cn/api";
var App_Name = "/version/getNewAppVersionName";
var Radio_list = "/radioStation/listRadio";
var appDownload = "http://139.198.15.159/apk/";
var Save_HelpInfo = "/seekHelpInfo/save";

// Dom7
var $$ = Dom7;
var u = navigator.userAgent;
// Framework7 App main instance
var app  = new Framework7({
  root: '#app', // App root element
  id: 'io.framework7.testapp', // App bundle ID
  name: 'Framework7', // App name
  theme: 'auto', // Automatic theme detection
  view: {
    stackPages: true
  },
  //App routes
  routes: routes,
});

var homeView = app.views.create('#view-home', {
  url: '/'
});


var uploadDownloadUrl = '';
getAppName();

function getAppName() {
  $$('#map-ImageUrl').attr("width",($$(document.body).outerWidth(true))-30);
  app.request.get(BASE_API_URL+App_Name ,function (data) {
    var dataJson = eval("(" + data + ")");
    uploadDownloadUrl = appDownload+dataJson.data.packageName;
    // $$('.upload').attr("href",url);
    // $$('.upload').attr("download","优码客FM");
    // $$('.upload').attr("class","upload-app external");
  })
}

function testApp() {
  var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
  if(isiOS){
    window.location.href = "wx2443686472ab3fde://";
    $$('#upload').attr("href",'https://itunes.apple.com/cn/app/qq/idxxxx?mt=8');
    setTimeout(function () {
      $$('#upload').click();
    },1000)
    setTimeout(function () {
      $$('#upload').attr("href","javascript:testApp()");
    },2000)
  } else {
    window.location.href = "umakrFM://";
    $$('#upload').attr("href",uploadDownloadUrl);
    $$('#upload').attr("download","优码客FM");
    $$('#upload').attr("class","upload-app external");
    setTimeout(function () {
      $$('#upload').click();
    },1000)
    setTimeout(function () {
      $$("#upload").removeAttr("download");
      $$("#upload").removeAttr("class");
      $$('#upload').attr("href","javascript:testApp()");
    },2000)
  }
}

// getAdr();
// function getAdr() {
//   $.ajax({
//     url: "http://192.168.40.22:9081/api/radioStation/listRadio",
//     type: "GET",
//     data:{type:'0', userId: '183' ,areaCode:'92',pageNo:'1',pageSize:'10'},
//     dataType: "jsonp", //指定服务器返回的数据类型
//     jsonpCallback: "IPMarker",    // 自定义回调函数名
//
//   });
//
//   // $.jsonp({
//   //   url:'http://192.168.40.22:9081/api/radioStation/listRadio',
//   //   data:{type:'0', userId: '183' ,areaCode:'92',pageNo:'1',pageSize:'10'},
//   //   callbackParameter:"callback",
//   //   timeout:3000,
//   //   dataFilter:function(json){
//   //     console.log(json);
//   //   },
//   //   success: function(data) {
//   //     console.log(data)
//   //   },
//   //   error: function(d,msg) {
//   //     console.log(msg)
//   //   }
//   // });
//
// }
// function IPMarker(res) {
//   console.log(res)
// }


// //获取求助电台
var ac3 = undefined;
app.request.getJSON(BASE_API_URL+Radio_list,{ type:'0', userId: '' ,areaCode:'92',pageNo:'1',pageSize:'10'}, function (data) {
  // $$('.index').html(data);
  // console.log(data);
  $$("a").attr("value",data.data.list[0].rsId);
  $$('#rsName').html(data.data.list[0].rsName);
  var html = '';
  var arrayObj = new Array();
  var jsonstr="[]";
  var jsonarray = eval('('+jsonstr+')');
  for(var i=0;i<data.data.list.length;i++){
    var id = data.data.list[i].rsId
    var text = data.data.list[i].rsName;
    html = '<li id='+id+' style="font-size: large" value='+text+' onclick="getPaging('+id+');">'+text+' </li>';
    $$('.simple-list ul').append(html);
  }
  if(data.data.list.length<10){
    $$('.infinite-scroll-preloader').remove();
  }
});

var getPaging = function (id) {
  var ul = $$("#UlLi").find("li");
  for(var i=0;i<ul.length;i++){
    var cityid = ul[i].getAttribute("id");
    if(id == cityid) {
      var value = ul[i].getAttribute("value");
      break;
    }
  }
  $$("a").attr("value",id);
  $$('#rsName').html(value);
  $$('#popup-close').click();
}


//发布求助信息
$$('.convert-form-to-data').on('click', function(){
  var formData = app.form.convertToData('#my-form');
  if(formData.seekerName === '' || ((formData.seekerName).match(/^[ ]+$/))) {
    $$('#tips').html('* 请填写求助人姓名');
    return;
  }
  if(formData.seekTitle === '' || ((formData.seekTitle).match(/^[ ]+$/))){
    $$('#tips').html('* 请填写求助标题');
    return;
  }
  if(formData.seekDes === '' || ((formData.seekerName).match(/^[ ]+$/))){
    $$('#tips').html('* 请填写求助描述');
    return;
  }
  if(formData.contacts === '' || ((formData.contacts).match(/^[ ]+$/))){
    $$('#tips').html('* 请填写联系人姓名');
    return;
  }
  if(formData.contactsTell === '' || ((formData.contactsTell).match(/^[ ]+$/))){
    $$('#tips').html('* 请填写联系电话');
    return;
  }
  var mapData = window.isSaveAdr?{lng:window.markerLongitude, lat:window.markerLatitude}:null;
  mapData =mapData? JSON.stringify(mapData):null;

  var addr = window.isSaveAdr?{name:window.helpPositionName,fname:window.helpPositionFullName}:null;
  addr = addr?JSON.stringify(addr):null;

  formData.rsId = $$("#rsName").attr("value");
  formData.userId = -2;
  formData.seekVoice = '';
  formData.addrCoordinate = mapData;
  formData.addrStr = addr;
  formData.photoInfo = JSON.stringify(photoInfo);
  formData.radioLen = '';
  formData.drivingDirection = '';
  formData.webApp = 0;
  var seekType = $$("#seekType").html();
  if(seekType === '寻人') {
    formData.seekType = '1';
  } else if(seekType === '寻物') {
    formData.seekType = '2';
  }else if(seekType === '车辆求助') {
    formData.seekType = '3';
  }else if(seekType === '寻找失主') {
    formData.seekType = '4';
  }else{
    formData.seekType = '5';
  }
  // alert(JSON.stringify(formData));
  app.request.post(BASE_API_URL+Save_HelpInfo,formData, function (data) {
    var testJson = eval("(" + data + ")");
    if(testJson.errorCode === 1){
      app.dialog.create({
        title: '提示',
        text: testJson.msg,
        buttons: [
          {
            text: '确定',
            onClick:function () {
              location.reload();
            }
          }
        ],
      }).open();
    }
    // $$('.index').html(data);

    console.log(testJson);
  });
});

// $$('.upload-app').on('click', function(){
//
//   // $.ajax({
//   //   url: 'http://139.198.15.159/apk/优码客FM-releaseV1.0.1.apk',
//   //   tag: "ajaxAPI",
//   //   method: 'get',
//   //   dataType: 'jsonp'
//   // }, function(ret, err) {
//   //   console.log(err)
//   // });
//
// })

var ac1 = app.actions.create({
  buttons: [
    [
      {
        text: '寻人',
        onClick: function () {
          $$('#seekType').html('寻人');
        }
      },
      {
        text: '寻物',
        onClick: function () {
          $$('#seekType').html('寻物');
        }
      },
      {
        text: '车辆求助',
        onClick: function () {
          $$('#seekType').html('车辆求助');
        }
      },
      {
        text: '寻找失主',
        onClick: function () {
          $$('#seekType').html('寻找失主');
        }
      },{
      text: '其他',
      onClick: function () {
        $$('#seekType').html('其他');
      }
    }
    ],
    [
      {
        text: '取消',
      }
    ]
  ]
});

//求助类型
$$('.ac-1').on('click', function () {
  ac1.open();
  setTimeout(function () {
    $$(".actions-button").attr("class","actions-button no-ripple");
  },500)
});


// Loading flag
var allowInfinite = true;
// Last loaded index
var lastItemIndex = 10;
var itemsPerLoad = 10;
var page = 1
// Attach 'infinite' event handler
$$('.infinite-scroll-content').on('infinite', function () {
  // Exit, if loading in progress
  if (!allowInfinite) return;

  // Set loading flag
  allowInfinite = false;

  // Emulate 1s loading
  setTimeout(function () {
    page++;
    // Reset loading flag
    allowInfinite = true;
    app.request.getJSON(BASE_API_URL+Radio_list,{ type:'0', userId: '183' ,areaCode:'92',pageNo:page,pageSize:'10'}, function (data) {
      // $$('.index').html(data);
      // console.log(data);
      $$("a").attr("value",data.data.list[0].rsId);
      $$("#rsName").html(data.data.list[0].rsName);
      var html = '';
      var arrayObj = new Array();
      var jsonstr="[]";
      var jsonarray = eval('('+jsonstr+')');
      for(var i=0;i<data.data.list.length;i++){
        var id = data.data.list[i].rsId

        var text = data.data.list[i].rsName;
        html = '<li id='+id+' style="font-size: large" value='+text+' onclick="getPaging('+id+');">'+text+' </li>';
        $$('.simple-list ul').append(html);
      }
      if(jsonarray.length<10) {
        $$('.infinite-scroll-preloader').remove();
      }
    });
  }, 1000);
});


